package com.renwei.yunlong.fragment;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.text.TextUtils;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseLazyFragment;
import com.renwei.yunlong.bean.NewDetailsAttributesBean;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * 对象详情  属性  页面
 * <p>
 * Created by Administrator on 2017/9/7.
 */

@SuppressLint("ValidFragment")
public class ObjectAttributesFragment extends BaseLazyFragment implements HttpTaskListener {

    @Bind(R.id.ll)
    LinearLayout ll;

    private String outSourceFlag = "";
    private String assetId;
    public NewDetailsAttributesBean serviceBean;

    public ObjectAttributesFragment(String assetId, String outSourceFlag) {
        this.assetId = assetId;
        this.outSourceFlag = outSourceFlag;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_new_details_attributes;
    }

    @Override
    public void onLazyLoad() {
        initData();
    }


    public void initData() {
        HashMap<String, String> map = new HashMap<>();
        if ("1".equals(companyType)) {//业主
            if ("0".equals(outSourceFlag)) {
                map.put("currentUserId", ownerBean.getRows().getEmployeeId());
                map.put("assetId", assetId);
            } else {//外协单调用这个接口
                map.put("assetId", assetId);
            }
        } else if ("2".equals(companyType)) {//服务商
            map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
            map.put("assetId", assetId);
        }
        ServiceRequestManager.getManager().getObjectDetail(getContext(), outSourceFlag, JsonMapUtil.mapToJson(map), this);
    }

    @Override
    public void onSuccess(int requestId, String result) {
        serviceBean = new Gson().fromJson(result, NewDetailsAttributesBean.class);
        if (serviceBean.getMessage().getCode()==200 && serviceBean.getRows() != null) {
            Map<String, String> map = serviceBean.getRows();
            for (Map.Entry<String, String> entry : map.entrySet()) {
                //动态生成view
                if ("assetId".equals(entry.getKey())
                        ||"ownerCode".equals(entry.getKey())
                        ||"serverName".equals(entry.getKey())
                        ||"bindFlag".equals(entry.getKey())){
                    continue;
                }
                RelativeLayout rl = new RelativeLayout(getContext());
                LinearLayout.LayoutParams rlParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        DensityUtil.dp2px(49));
                rl.setLayoutParams(rlParams);
                rl.setPadding(DensityUtil.dp2px(15),0,0,0);
                rl.setBackgroundColor(Color.WHITE);

                TextView tv_title = new TextView(getContext());
                RelativeLayout.LayoutParams tv_title_params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
                tv_title_params.addRule(RelativeLayout.CENTER_VERTICAL);
                tv_title.setTextSize(15);
                tv_title.setTextColor(getResources().getColor(R.color.color_666666));
                tv_title.setLayoutParams(tv_title_params);
                tv_title.setText(entry.getKey());

                TextView tv_content = new TextView(getContext());
                RelativeLayout.LayoutParams tv_content_params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
                tv_content_params.addRule(RelativeLayout.CENTER_VERTICAL);
                tv_content_params.setMargins(DensityUtil.dp2px(85),0,DensityUtil.dp2px(10),0);
                tv_content.setLayoutParams(tv_content_params);
                tv_content.setTextSize(15);
                tv_content.setTextColor(getResources().getColor(R.color.color_333333));
                tv_content.setLines(1);
                tv_content.setEllipsize(TextUtils.TruncateAt.END);
                tv_content.setTextIsSelectable(true);
                tv_content.setText(entry.getValue());

                rl.addView(tv_title);
                rl.addView(tv_content);
                getLayoutInflater().inflate(R.layout.line_view,rl,true);
                ll.addView(rl);
            }
        } else if (serviceBean.getMessage().getCode() == 1008) {
            showCenterInfoMsg("无权限访问");
        }
    }

    @Override
    public void onException(int requestId, String code) {
        showTopWrongMsg(getResources().getString(R.string.net_work_error));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
